package com.bw.module_detail.fragment

import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.navArgs
import com.bumptech.glide.Glide
import com.bw.lib_common.base.BaseMVIFragment
import com.bw.module_detail.adapter.DetailVideoAdapter
import com.bw.module_detail.databinding.FragmentVideoBinding
import com.bw.module_detail.intent.DetailIntent
import com.bw.module_detail.state.DetailSimpleState
import com.bw.module_detail.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class VideoFragment : BaseMVIFragment() {
    private val mBinding:FragmentVideoBinding by lazy {
        FragmentVideoBinding.inflate(layoutInflater)
    }
    private lateinit var mViewModel:DetailViewModel
    private val detailAdapter = DetailVideoAdapter()

    override fun generateViewModel() {
        mViewModel = ViewModelProvider(this)[DetailViewModel::class.java]
    }


//         fun updateUI(icon:String,name:String,desc:String,num:String) {
//             mBinding.icon.setImageURI(Uri.parse(icon))
//             mBinding.userName.text = name
//             mBinding.desc.text = desc
//             mBinding.count.text = num
//             //打印
//             Log.d("hahaha", "updateUI: $icon $name $desc $num")
//         }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null){
//            mBinding.icon.setImageURI(Uri.parse(requireArguments().getString("icon") ))
//
            Glide.with(this)
                .load((requireArguments().getString("icon")))
                .into(mBinding.icon)
             mBinding.userName.text = requireArguments().getString("name")
             mBinding.desc.text = requireArguments().getString("desc")
             mBinding.count.text = requireArguments().getString("num")
        }
    }
    override fun initView() {
        super.initView()
        mBinding.rvVideo.adapter = detailAdapter
        detailAdapter.notifyDataSetChanged()
        //点击事件
        mBinding.live.setOnClickListener {
            //进行点赞的操作

        }
        mBinding.guan.setOnClickListener {
            //进行关注操作

        }
        mBinding.share.setOnClickListener{
            //进行分享操作

        }
        mBinding.star.setOnClickListener {
            //进行收藏操作

        }
        mBinding.dashang.setOnClickListener {
            //进行打赏操作

        }
    }

    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                // 从Activity获取视频ID和数据类型
                val videoId = (activity as? com.bw.module_detail.MainActivity)?.itemId ?: ""
                val dataType = (activity as? com.bw.module_detail.MainActivity)?.dataType ?: 0
                
                // 使用获取到的视频ID和数据类型请求相关视频列表
                if (videoId.isNotEmpty()) {
                    mViewModel.intents.send(DetailIntent.getVideoList(1, 10))
                    // 后续可以根据需要将videoId和dataType传递给相关API
                }
            }
        }
    }
    override fun getFragmentRootView(): View {
        return mBinding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                mViewModel.videoState_.collect{
                    when(it){
                        is DetailSimpleState.Init ->{

                        }
                        is DetailSimpleState.Error ->{

                        }
                        is DetailSimpleState.Empty ->{

                        }
                        is DetailSimpleState.Success ->{
                            Log.d("TAG", "handleState: ${it.data}")
                                detailAdapter.setData(it.data)
                        }
                        is DetailSimpleState.Loading ->{

                        }
                    }
                }
            }
        }
    }
}